package com.knoya.chejian.pda.module.zupan

import android.content.Context
import com.knoya.chejian.pda.contacts.Contacts
import com.knoya.chejian.pda.minterface.OnUserDataInterface
import com.knoya.chejian.pda.util.HttpUtil
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import org.xutils.http.RequestParams
import org.xutils.x

class ZupanM {
    fun getdata(context : Context,code : String,pcode : String,onuserdatainterface : OnUserDataInterface){
        var stat = "category"
        when(code){
            "按订单" -> {
                stat = "order"
            }
            "按柜号" -> {
                stat = "cabinet"
            }
            "按包号" ->{
                stat = "package"
            }
        }
        val params = RequestParams(Contacts.ZUPAN_XIAZAI)
        HttpUtil.getparams(context, params)
        HttpUtil.addtoken(params)
        params.addBodyParameter("bath", stat.trim(), Contacts.BOUNDARY)
        params.addBodyParameter("pcode", pcode.trim(), Contacts.BOUNDARY)
        params.addHeader("ACCEPT", "application/json")
        x.http().post<String>(params, HttpUtil.Call(context,onuserdatainterface))
    }

    fun postzupan(context: Context,gaodu : String,weizhi:String,pallet_num : String,mlist: List<ZupanBean>,onuserdatainterface : OnUserDataInterface){
        val params = RequestParams(Contacts.ZUPAN)
        HttpUtil.getparams(context, params)
        HttpUtil.addtoken(params)
        if(gaodu.trim() != ""){
            params.addBodyParameter("height",gaodu.trim()+"mm",Contacts.BOUNDARY)
        }
        params.addBodyParameter("position_buffer", weizhi.trim(), Contacts.BOUNDARY)

        params.addBodyParameter("pallet_num",pallet_num.trim(),Contacts.BOUNDARY)
        for (i in mlist.indices){
            params.addBodyParameter("pcodes[$i]",mlist[i].pcode.trim(),Contacts.BOUNDARY)
        }
        params.addHeader("ACCEPT", "application/json")
        x.http().post<String>(params, HttpUtil.Call(context,onuserdatainterface))
    }

    fun deletezupan(context: Context,mlist: List<ZupanBean>,onuserdatainterface :OnUserDataInterface){
        val params = RequestParams(Contacts.ZUPAN_DELETE)
        HttpUtil.getparams(context, params)
        HttpUtil.addtoken(params)
        params.addBodyParameter("","",Contacts.BOUNDARY)
        for (i in mlist.indices){
            params.addBodyParameter("pcodes[$i]",mlist[i].pcode.trim(),Contacts.BOUNDARY)
        }
        params.addHeader("ACCEPT", "application/json")
        x.http().post<String>(params, HttpUtil.Call(context,onuserdatainterface))
    }

    fun yipan(context: Context,pallet_num: String,buffer_finish_status:String,buffer_entrance_status:String,onuserdatainterface :OnUserDataInterface){
        val params = RequestParams(Contacts.YIPAN)
        HttpUtil.getparams(context, params)
        HttpUtil.addtoken(params)
        var jo = JSONObject()
        try {
            var js = JSONArray()
            var joo = JSONObject()
            joo.put("pallet_num",pallet_num)
            joo.put("buffer_finish_status",buffer_finish_status)
            joo.put("buffer_entrance_status",buffer_entrance_status)
            js.put(joo)
            jo.put("pallet_buffer",js)
        }catch (e : JSONException){
            e.printStackTrace()
        }
        params.bodyContent = jo.toString()

        params.addHeader( "Content-Type", "application/json")
        x.http().post<String>(params, HttpUtil.Call(context,onuserdatainterface))
    }
}